#include "app_config.h"
#include "board_ac6323a_demo_cfg.h"
#include "asm/charge.h"
#include "asm/mcpwm.h"
#include "asm/pwm_led.h"
#include "system/event.h"
#include "system/app_core.h"
#include "system/includes.h"
#include "app_action.h"
#include "asm/wdt.h"
#include "app_main.h"
#include "app_power_manage.h"
#include "app_handshake.h"

#define LOG_TAG_CONST       APP_CHARGE
#define LOG_TAG             "[APP_CHARGE]"
#define LOG_ERROR_ENABLE
#define LOG_DEBUG_ENABLE
#define LOG_INFO_ENABLE
/* #define LOG_DUMP_ENABLE */
#define LOG_CLI_ENABLE
#include "debug.h"

#if TCFG_CHARGE_ENABLE

#if TCFG_HANDSHAKE_ENABLE
static void handshake_complete(void)
{
    handshake_app_stop();
    if (get_lvcmp_det()) {
        charge_start();
    }
}
#endif

//这个是开启充电
void charge_start_deal(void)
{
    log_info("%s\n", __func__);
    power_set_mode(PWR_LDO15);
}

void ldo5v_keep_deal(void)
{
    log_info("%s\n", __func__);
#if TCFG_HANDSHAKE_ENABLE
    handshake_app_start(2, handshake_complete);
#endif
}

void charge_full_deal(void)
{
    log_info("%s\n", __func__);
    charge_close();
}

void charge_close_deal(void)
{
    log_info("%s\n", __FUNCTION__);
}

void charge_ldo5v_in_deal(void)
{
    log_info("%s\n", __FUNCTION__);
#if TCFG_HANDSHAKE_ENABLE
    handshake_app_start(2, handshake_complete);
#else
    charge_start();
#endif
}

void charge_ldo5v_off_deal(void)
{
    log_info("%s\n", __FUNCTION__);
#if TCFG_HANDSHAKE_ENABLE
    handshake_app_stop();
#endif
    charge_close();
    power_set_mode(TCFG_LOWPOWER_POWER_SEL);
#if TCFG_SYS_LVD_EN
    vbat_check_init();
#endif
}

extern u8 SYS_Flag,LED_Show_Ci,LED_8_Data,VBAT_CD_Full_Delay20S,DC_5V_IN_Flag,BT_ON_FLAG;
extern u16 LED_2ms_ID;
extern void Buzzer_ON(u8 Type);
extern void LED_Show_500ms(void *priv);

int app_charge_event_handler(struct device_event *dev)
{
    switch (dev->event) {

    case CHARGE_EVENT_CHARGE_START://开始充电
        charge_start_deal();
        DC_5V_IN_Flag=1;
        LED_Show_Ci=0;
        LED_8_Data=1;
#if TCFG_USER_BLE_ENABLE
        if(BT_ON_FLAG==1)//蓝牙进程已经开启需要关闭
        {
           btstack_ble_exit(0);  
           log_info("btstack_ble_exit OK \n");
           BT_ON_FLAG=0;
        }
#endif

        if(SYS_Flag==1)//开机的情况下进来的
        {
          EMS_ALL_OFF();//关闭EMS
          FD_ON();//开放电
        }
        SYS_Flag=4;
        Buzzer_ON(1);//蜂鸣器开启响1声
        if(LED_2ms_ID==0)
        {
          LED_2ms_ID=sys_hi_timer_add(NULL, LED_Show_500ms, 500);//开启LED强度数显扫描功能
        }

        break;
    case CHARGE_EVENT_CHARGE_CLOSE://关闭充电
        charge_close_deal();
        //power_set_soft_poweroff();//关机进入超低功耗
        break;
    case CHARGE_EVENT_CHARGE_FULL://充满电了
        DC_5V_IN_Flag=1;
        VBAT_CD_Full_Delay20S=0;
        charge_full_deal();
        SYS_Flag=5;
        break;
    case CHARGE_EVENT_LDO5V_KEEP:
        ldo5v_keep_deal();
        break;
    case CHARGE_EVENT_LDO5V_IN://插入充电
        charge_ldo5v_in_deal();
        break;

    case CHARGE_EVENT_LDO5V_OFF: //拔插充电
        DC_5V_IN_Flag=0;
        charge_ldo5v_off_deal();
        if((SYS_Flag==4)||(SYS_Flag==5))//只有等于4或者5的时候才判断这个
        {
           SYS_Flag=0;
           LED_OFF();
           power_set_soft_poweroff();//关机进入超低功耗
        }
        break;
    default:
        break;
    }
    return 0;
}

#endif

